/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m001.bl;

import ep.persistence.controllers.T001UserJpaController;
import ep.persistence.entities.T001User;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Spyros
 */
public class BL001Controller {

    public Short login(String username, String password) {
        T001User user = getUserFromUsername(username);
        if (user != null) {
            if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
                return user.getType();
            }
        }
        return null;
    }

    public T001User getUserFromUsername(String username) {
        T001UserJpaController t001UserJpaController = new T001UserJpaController();
        Query query = t001UserJpaController.getEntityManager().createNamedQuery("T001User.findByUsername", T001User.class);
        query.setParameter("username", username);
        List result = query.getResultList();
        if (!result.isEmpty()) {
            return (T001User) query.getResultList().iterator().next();
        } else {
            return null;
        }
    }
}
